
#include "GlutWindow.h"
#include "Engine.h"
#include <GL/glut.h>
#include <iostream>

// --------------------------------------------------------------------------------------
GlutWindow::GlutWindow(const WindowSettings& settings )
	: WindowInterface(settings), reverseKeyMap()
{
	// inicalização
	glutInit( &(this->settings.argc), this->settings.argv );
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );

	// criação da janela
	glutInitWindowPosition( 100,100 );
	glutInitWindowSize( settings.width, settings.height );
	glutCreateWindow( settings.title.c_str() );

	//
	glutKeyboardFunc( GlutWindow::keyboardFunc  );
	glutKeyboardUpFunc( GlutWindow::keyboardUpFunc );
	glutReshapeFunc( GlutWindow::reshapeFunc );
	glutDisplayFunc( GlutWindow::displayFunc );
	glutIdleFunc( GlutWindow::idleFunc );

	// mapeamento de teclas
	reverseKeyMap[ 32 ] = WindowInterface::KEYBOARD_SPACE;
	// otimo
	reverseKeyMap[ 97 ] = WindowInterface::KEYBOARD_LEFT;
	reverseKeyMap[ 119 ] = WindowInterface::KEYBOARD_UP;
	reverseKeyMap[ 100 ] = WindowInterface::KEYBOARD_RIGHT;
	reverseKeyMap[ 115 ] = WindowInterface::KEYBOARD_DOWN;

	reverseKeyMap[ 13 ] = WindowInterface::KEYBOARD_ENTER;
	reverseKeyMap[ 27 ] = WindowInterface::KEYBOARD_ESC;
}
// --------------------------------------------------------------------------------------
GlutWindow::~GlutWindow(void)
{
}
// --------------------------------------------------------------------------------------
void GlutWindow::startRendering(void)
{
	glutMainLoop();
}
// --------------------------------------------------------------------------------------
void GlutWindow::swapBuffers(void)
{
	glutSwapBuffers();
	glutPostRedisplay();
}
// --------------------------------------------------------------------------------------
void GlutWindow::keyboardFunc( unsigned char key, int x, int y )
{
	WindowInterface* wi = Engine::getInstance().getWindow();
	if( wi )
	{
		GlutWindow* gluw = dynamic_cast<GlutWindow*>(wi);
		if( gluw->reverseKeyMap.find(key) != gluw->reverseKeyMap.end() )
		{
			gluw->keyMap[ gluw->reverseKeyMap[key] ] = true;
		}
	}
}
// --------------------------------------------------------------------------------------
void GlutWindow::keyboardUpFunc( unsigned char key, int x, int y )
{
	WindowInterface* wi = Engine::getInstance().getWindow();
	if( wi )
	{
		GlutWindow* gluw = dynamic_cast<GlutWindow*>(wi);
		if( gluw->reverseKeyMap.find(key) != gluw->reverseKeyMap.end() )
		{
			gluw->keyMap[ gluw->reverseKeyMap[key] ] = false;
		}
	}
}
// --------------------------------------------------------------------------------------
void GlutWindow::reshapeFunc( int width, int height )
{
	WindowInterface* wi = Engine::getInstance().getWindow();
	if( wi )
	{
		wi->resize(width, height);
	}
}
// --------------------------------------------------------------------------------------
void GlutWindow::displayFunc(void)
{
	Engine::getInstance().drawScene();
}
// --------------------------------------------------------------------------------------
void GlutWindow::idleFunc(void)
{
	// TODO usar isso para alguma coisa... networking, por exempo
}
// --------------------------------------------------------------------------------------
